package com.zdpx.pxframework.datapermission.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.zdpx.pxframework.core.constant.SecurityConstant;
import com.zdpx.pxframework.core.pojo.LoginUser;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.datapermission.entity.DataPermissionResult;
import com.zdpx.pxframework.datapermission.service.DataPermissionFrameworkService;
import com.zdpx.pxframework.system.api.api.RemoteDepartmentInfoService;
import com.zdpx.pxframework.system.api.api.RemoteRoleDepartmentDataScopeService;
import com.zdpx.pxframework.system.api.api.RemoteRoleInfoService;
import com.zdpx.pxframework.system.api.entity.RoleInfo;
import com.zdpx.pxframework.system.api.enums.DataPermissionEnum;

import javax.annotation.Resource;
import java.util.*;

/**
 * 部门数据权限处理类
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022/5/27 14:40
 */
public class DeptDataPermissionFrameworkServiceImpl implements DataPermissionFrameworkService {

    @Resource
    private RemoteRoleInfoService remoteRoleInfoService;
    @Resource
    private RemoteRoleDepartmentDataScopeService remoteRoleDepartmentDataScopeService;
    @Resource
    private RemoteDepartmentInfoService remoteDepartmentInfoService;

    /**
     * 根据用户信息获取部门权限信息
     * @param loginUser 登录用户
     * @return 部门权限信息
     */
    @Override
    public DataPermissionResult getDeptDataPermission(LoginUser loginUser) {
        //获取角色信息
        Result<List<RoleInfo>> roleListResult = remoteRoleInfoService.listByRoleKeys(new ArrayList<>(loginUser.getRoles()), SecurityConstant.INNER);

        DataPermissionResult dataPermissionResult = new DataPermissionResult();
        for (RoleInfo roleInfo : roleListResult.getData()) {
            // 数据范围为空
            if (roleInfo.getDataScope() == null) {
                continue;
            }

            //1. 如果有全部数据权限
            if (Objects.equals(DataPermissionEnum.ALL.getScope(), roleInfo.getDataScope())) {
                dataPermissionResult.setIsAll(true);
                continue;
            }

            //2. 自定义数据权限
            if (Objects.equals(DataPermissionEnum.DEPT_CUSTOM.getScope(), roleInfo.getDataScope())) {
                Result<List<String>> deptListResult = remoteRoleDepartmentDataScopeService.listDeptsByRoleIds(ListUtil.toList(roleInfo.getId()), SecurityConstant.INNER);
                //添加自定义的和自己的部门
                CollUtil.addAll(dataPermissionResult.getDeptIds(), deptListResult.getData());
                CollUtil.addAll(dataPermissionResult.getDeptIds(), loginUser.getDepartmentIds());
                continue;
            }
            //3. 只能查看自己部门数据
            if (Objects.equals(DataPermissionEnum.DEPT_ONLY.getScope(), roleInfo.getDataScope())) {
                dataPermissionResult.setDeptIds(loginUser.getDepartmentIds());
                continue;
            }
            // 部门及子部门
            if (Objects.equals(DataPermissionEnum.DEPT_AND_CHILD.getScope(), roleInfo.getDataScope())) {
                for (String departmentId : loginUser.getDepartmentIds()) {
                    Result<List<String>> childrenIdResult = remoteDepartmentInfoService.listChildrenIdsByParentId(departmentId, SecurityConstant.INNER);
                    CollUtil.addAll(dataPermissionResult.getDeptIds(), childrenIdResult.getData());
                }
                CollUtil.addAll(dataPermissionResult.getDeptIds(), loginUser.getDepartmentIds());
                continue;
            }
            // 情况五，SELF
            if (Objects.equals(DataPermissionEnum.SELF.getScope(), roleInfo.getDataScope())) {
                dataPermissionResult.setIsSelf(true);
            }
        }

        return dataPermissionResult;
    }
}
